package com.yq.design.proxy.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName TeachDynamicProxy
 * @Description 动态代理工厂类
 * @Author yaoqiang
 * @Date 2021/3/1 11:24
 * @Version 1.0
 */
public class DynamicProxyFactory {
    private Object target;

    public DynamicProxyFactory(Object target) {
        this.target = target;
    }

    //给目标对象生成一个代理对象
    public Object getProxyInstance(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("JDK代理开始");
                //反射机制调用目标对象的方法
                Object invoke = method.invoke(target, args);
                System.out.println("JDK代理结束");
                return invoke; }}); }



}
